﻿using System;
using System.Web;
using System.Web.Security;
using DreamDriven.ObjectModel.Mvp;

namespace DreamDriven.Web
{
    /// <summary>
    /// Wrapper for WebFormsMvp.Web.MvpHttpHandler
    /// </summary>
    public abstract class BaseHttpHandler : IHttpHandler, IView
    {
        /// <summary>
        /// Gets the name of the current user.
        /// </summary>
        /// <value>The name of the current user.</value>
        public string CurrentUserName
        {
            get { return HttpContext.Current.User.Identity.Name; }
        }

        /// <summary>
        /// Gets the current user id.
        /// </summary>
        /// <value>The current user id.</value>
        public object CurrentUserId
        {
            get
            {
                MembershipUser user = Membership.GetUser(CurrentUserName);
                if (user != null)
                {
                    return user.ProviderUserKey;
                }
                return null;
            }
        }

        /// <summary />
        public void ProcessRequest(HttpContext context)
        {
            if (Load != null)
            {
                Load(this, EventArgs.Empty);
            }
        }

        /// <summary>
        /// Gets a value indicating whether another request can use the <see cref="T:System.Web.IHttpHandler"/> instance.
        /// </summary>
        /// <value></value>
        /// <returns>true if the <see cref="T:System.Web.IHttpHandler"/> instance is reusable; otherwise, false.</returns>
        public virtual bool IsReusable
        {
            get { return false; }
        }

        /// <summary>
        /// Occurs during the <see cref="ProcessRequest"/> method.
        /// </summary>
        public event EventHandler Load;
        
        /// <summary>
        /// Raises the <see cref="Load"/> event.
        /// </summary>
        protected virtual void OnLoad()
        {
            if (Load != null)
            {
                Load(this, EventArgs.Empty);
            }
        }

        protected BaseHttpHandler()
        {
            PresenterBinder.Bind(this);
        }

    }
}